﻿using System;
using System.Windows;
using System.Windows.Controls;
using Kiva7.Settings;
using Kiva7.Utilities;
using Kiva7.ViewModels;
using Microsoft.Phone.Tasks;
using Kiva7.Controls;
using Kiva7.Translations;

namespace Kiva7
{
    public partial class SettingPage
    {
        public readonly static Uri PageUrl = new Uri("/Pages/SettingPage.xaml", UriKind.RelativeOrAbsolute);

        public SettingPage()
        {
            InitializeComponent();
            CreateStandardControls();
        }

        private void PhoneApplicationPageBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            DataController.CheckLenderId();
        }

        private void GetLenderIdClick(object sender, RoutedEventArgs e)
        {
            //Scraper scraper = new Scraper();
            //scraper.Scrape();

            WebBrowserTask task = new WebBrowserTask();
            task.Uri = new Uri("http://www.kiva.org/myLenderId");
            task.Show();
        }

        private void PhoneApplicationPageLoaded(object sender, RoutedEventArgs e)
        {
            if (lpTranslateTexts.Items.Count == 0)
            {
                lpTranslateTexts.Items.Add(new TranslatorOptionListPickerItem(AppResources.NeverTranslateTexts,
                                                                              TranslatorOption.Never));
                lpTranslateTexts.Items.Add(new TranslatorOptionListPickerItem(AppResources.AutomaticallyTranslateTexts,
                                                                              TranslatorOption.Automatic));

            }

            foreach(object item in lpTranslateTexts.Items)
            {
                if (AppSettings.Current.TranslationSetting == ((TranslatorOptionListPickerItem) item).Value)
                {
                    lpTranslateTexts.SelectedItem = item;
                    break;
                }
            }
            
        }

        private void LpTranslateTextsSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lpTranslateTexts.SelectedItem != null
                && lpTranslateTexts.Items.Count > 1)
            {
                AppSettings.Current.TranslationSetting =
                    ((TranslatorOptionListPickerItem) lpTranslateTexts.SelectedItem).Value;
            }
        }

        private void ToggleSwitch_Checked(object sender, RoutedEventArgs e)    
        {
            tswShowBackgroundImage.Content = AppResources.Show;    
        }    
        
        private void ToggleSwitch_Unchecked(object sender, RoutedEventArgs e)    
        {
            tswShowBackgroundImage.Content = AppResources.Hide;   
        }
    }
}